package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Map<String, Object>> findMenuList(String username) {
        List<Menu> menuList = menuDao.findMenuList(username);
        List<Map<String,Object>> list = new ArrayList<>();
        for (Menu menu : menuList) {
            Map<String, Object> map = new HashMap<>();
            String path = menu.getPath();
            String name = menu.getName();
            String icon = menu.getIcon();
            String linkUrl = menu.getLinkUrl();
            List<Menu> children = menu.getChildren();
            map.put("path",path);
            map.put("title",name);
            map.put("icon",icon);
            map.put("linkUrl",linkUrl);
            map.put("children",children);
            list.add(map);
        }
        return list;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        Page<Menu> page = menuDao.findPage();
        return new PageResult(page.getTotal(),page.getResult());

    }

    @Override
    public void add(Menu menu, Integer[] roleIds) {
        //获取前台新增父菜单ID
        Integer parentMenuId = menu.getParentMenuId();
        //判断是否有父菜单,如果为null，则没有父菜单，为一级菜单，则菜单等级level为1
        if (parentMenuId == null){
            //通过菜单等级1，获取所有父菜单路径path
            List<String> stringList = menuDao.findMenuByLevel();
            //获取list索引
            int size = stringList.size()-1;
            //获取最后一个元素，并添加新增一级菜单路径path
            Integer i = Integer.valueOf(stringList.get(size))+1;
            menu.setPath(String.valueOf(i));
            //设置菜单等级
            menu.setLevel(1);
            //设置菜单优先级
            menu.setPriority(Integer.valueOf(stringList.get(size)));
        }else {//否则为二级菜单
            //设置菜单等级level为2
            menu.setLevel(2);
            //根据父菜单id,获取所有该父菜单下，子菜单的优先级
            List<Integer> prioritys = menuDao.findMenuByParentMenuId(parentMenuId);
            //如果该父菜单下有子菜单，则对menu表中字段进行拼接
            if(prioritys != null && prioritys.size() > 0) {
                int size = prioritys.size() - 1;
                Integer priority = prioritys.get(size) + 1;   //   http://slither.io/
                //Integer priority = prioritys.size() + 1;
                menu.setPriority(priority);
                String path = menuDao.findPathById(parentMenuId);
                String path2 = "/" + path + "-" + priority;
                menu.setPath(path2);
            }else {
                //如果父菜单没有子菜单，则直接设置优先级为1，并拼接路径path
                menu.setPriority(1);
                String path = menuDao.findPathById(parentMenuId);
                String path2 = "/" + path + "-" + "1";
                menu.setPath(path2);
            }
        }
        menuDao.add(menu);
        Integer menuId = menu.getId();
        this.setRoleAndMenu(menuId, roleIds);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu, Integer[] roleIds) {
        Integer parentMenuId = menu.getParentMenuId();
        Integer id = menu.getId();
        Integer parentMenuId2 = menuDao.findParentMenuIdByID(id);
        if (parentMenuId != parentMenuId2){
            if (parentMenuId == null){
                List<String> stringList = menuDao.findMenuByLevel();
                int size = stringList.size()-1;
                Integer i = Integer.valueOf(stringList.get(size))+1;
                menu.setPath(String.valueOf(i));
                menu.setLevel(1);
                menu.setPriority(Integer.valueOf(stringList.get(size)));
            }else {
                menu.setLevel(2);
                List<Integer> prioritys = menuDao.findMenuByParentMenuId(parentMenuId);
                if(prioritys != null && prioritys.size() > 0) {
                    int size = prioritys.size() - 1;
                    Integer priority = prioritys.get(size) + 1;
                    menu.setPriority(priority);
                    String path = menuDao.findPathById(parentMenuId);
                    String path2 = "/" + path + "-" + priority;
                    menu.setPath(path2);
                }else {
                    menu.setPriority(1);
                    String path = menuDao.findPathById(parentMenuId);
                    String path2 = "/" + path + "-" + "1";
                    menu.setPath(path2);
                }
            }
            menuDao.edit(menu);
            menuDao.deleteAssociation(menu.getId());
            this.setRoleAndMenu(menu.getId(),roleIds);
        }
        menuDao.edit(menu);
        menuDao.deleteAssociation(menu.getId());
        this.setRoleAndMenu(menu.getId(),roleIds);
    }

    @Override
    public void delete(Integer id) {
        menuDao.deleteAssociation(id);
        menuDao.deleteMenu(id);
    }

    @Override
    public List<Map<String, String>> findOneMenu() {
        List<Menu> menuList = menuDao.findOneMenu();
        List<Map<String,String>> list = new ArrayList<>();
        for (Menu menu : menuList) {
            Map<String, String> map = new HashMap<>();
            String name = menu.getName();
            Integer menuId = menu.getId();
            map.put("name",name);
            map.put("id", String.valueOf(menuId));
            list.add(map);
        }
        return list;
    }


    public void setRoleAndMenu(Integer menuId, Integer[] roleIds) {
        if (roleIds != null && roleIds.length>0){
            for (Integer roleId : roleIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("menuId",menuId);
                map.put("roleId",roleId);
                menuDao.setRoleAndMenu(map);
            }
        }
    }



}
